﻿using System;
using System.IO;

namespace Toolkit.Web.Infastructure
{
    public class ViewComponentWriter : IViewComponentWriter
    {
        public ViewComponentWriter()
        {
            Writer = new StringWriter();
        }

        public ViewComponentWriter(IFormatProvider formatProvider)
        {
            Writer = new StringWriter(formatProvider);
        }

        public ViewComponentWriter(Action<object> writeDelegate)
        {
            if (writeDelegate == null)
            {
                throw new ArgumentNullException("writeDelegate");
            }

            WriteDelegate = writeDelegate;
        }

        public ViewComponentWriter(TextWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            Writer = writer;
        }

        public Action<object> WriteDelegate
        {
            get;
            private set;
        }

        public TextWriter Writer
        {
            get;
            private set;
        }

        public void Write(object value)
        {
            if (WriteDelegate != null)
            {
                WriteDelegate(value);
            }
            else
            {
                Writer.Write(value);
            }
        }
    }
}
